package dao

import (
	"context"
	"test.com/project-user/internal/data/organization"
	"test.com/project-user/internal/database"
	"test.com/project-user/internal/database/gorms"
)

type OrganizationDao struct {
	conn *gorms.GormConn
}

func (o *OrganizationDao) FindOrganizationByMemId(ctx context.Context, memId int64) ([]*organization.Organization, error) {
	//定义有*号使用的时候也要有取地址符号
	var orgs []*organization.Organization //这里是指针数组   他是一个数组  所以必须使用&锁定orgs这个变量的地址
	err := o.conn.Session(ctx).Where("member_id = ?", memId).Find(&orgs).Error
	return orgs, err
}

func NewOrganizationDao() *OrganizationDao {
	return &OrganizationDao{
		conn: gorms.New(),
	}
}

func (o *OrganizationDao) SaveOrganization(conn database.DbConn, ctx context.Context, org *organization.Organization) error {
	o.conn = conn.(*gorms.GormConn)
	//o.conn.Tx(ctx)表示开启一个事务Tx是一个带有事务的db对象
	err := o.conn.Tx(ctx).Create(org).Error
	return err
}
